package server;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.LinkedList;

import common.Tweet;

import client.ClientInterface;

public class Dispatcher implements Runnable {

	private String clientIP;
	private TweetStack stack;
	private boolean finished;
	private String name;
	private IdList ids;

	public String getClientIP() {
		return clientIP;
	}

	public Dispatcher(String clientIP, TweetStack stack, String name) {

		System.out.println("Some one created a dispatcher");
		this.clientIP = clientIP;
		System.out.println(this.clientIP);
		this.stack = stack;
		this.finished = false;
		this.name=name;
		this.ids = new IdList();

	}
	

	public TweetStack getStack() {
		return stack;
	}

	public synchronized boolean getFinished() {
		return this.finished;
	}

	public synchronized void setFinished(boolean b) {
		this.finished = b;
	}

	public boolean isAlive() {
		return !this.getFinished();
	}
	
	public boolean idCheck(Tweet t){
		System.out.println("Checkin tweet!");
		LinkedList<Long> check = ids.getlistStack();
		int range = check.size();
		System.out.println(check);
		System.out.println(range);
		for (int i =0 ; i < range; i ++){
			System.out.println(t.getId()+" == "+check.get(i));
			if (t.getId()==check.get(i)){
				System.out.println("Tweet allready exists");
				return false;
			}
		}
		System.out.println("New tweet arrived");
		ids.push(t.getId());
		IdDeleter d = new IdDeleter(ids);
		new Thread(d).start();
		return true;
	}

	public synchronized void send() {
		int tries = 0;
		Tweet t = stack.pop();
		if (!idCheck(t)){
			tries = 4;
			System.out.println("Tweet "+t.getId()+"  status:  "+t.getMsg()+"  already sent!");
		}
		while (tries<3){
			
			try {

				Registry registry = LocateRegistry.getRegistry(clientIP);
				ClientInterface stub = (ClientInterface) registry.lookup(name);
				stub.addTweet(t);
				System.out.println("Tweet "+t.getId()+" sent:  status:  "+t.getMsg());
				break;
				
			} catch (Exception e) {
				tries ++;
				System.out.println("Failed  "+tries+"  times");
				e.printStackTrace();
			}
		if (tries == 3){
			setFinished(true);
			stack.push(new Tweet());
		}
		
		}
		
		System.out.println("Tweet sent via dispatcher");

		// this method will actually use the client remote method to write a
		// tweet

	}

	public void run() {

		while (this.isAlive()) {
			if (stack.isEmpty()) {
				try {
					//Thread.sleep(10000);
					synchronized (stack){
						stack.wait();
					}
				} catch (InterruptedException e) {
					continue;
				}
			} else {
				send();
			}
		}

	}
	
	public synchronized void clientAlive(){
		int tries = 0;
		while (tries<3){
			
			try {
				Registry registry = LocateRegistry.getRegistry(clientIP);
				ClientInterface stub = (ClientInterface) registry.lookup(name);

				stub.isAlive();
				System.out.println("Client ip: "+this.clientIP+"  name:  "+this.name+" still alive");
				break;
				
			} catch (Exception e) {
				tries ++;
				System.out.println("Failed  "+tries+"  times");
				e.printStackTrace();
			}
		if (tries == 3){
			setFinished(true);
		}
		
		}
	}
	

}
